#title          : getURLsReceived.py
#description    : outputs a list of user, url for each of the url's that a user has received
#                 need this to formulate the receiving filtering curve
#author         :Arthi Ramachandran
#date           :20131231
#version        :1      
#usage          :python ./getURLsReceived.py <network file> <posts file>
#notes          :       
#python_version :2.7
#============================================================================
import csv
import sys
import datetime

urls = dict()
posts_file = sys.argv[2]
with open(posts_file, "rb") as f:
    csvreader = csv.reader(f, quoting=csv.QUOTE_NONE)
    for row in csvreader:
        url = row[1]
        user = row[0]
        if user not in urls:
            urls[user] = set()
        urls[user].add(url)        

# don't need to store the ppl counts - everything calculated at once
count = 0
pplcounts = dict()
urlcounts = dict()

# for each person; look at all the ppl they follow
# then print
current_follower = ""
num_followers = dict()
num_links_received = 0
links_received = set()
graph_filename = sys.argv[1]
count = 0
with open(graph_filename, "rb") as f: # follower -> followee - reverse in the nytimes dataset
    for line in f:
        count += 1
        #if count % 1000000 == 0:
            #print count
        fields = line.strip().split("\t")
        follower = fields[1]
        followee = fields[0]
        if current_follower == "":
            current_follower = follower # first time
        if follower != current_follower: # print out all the info about this follower
            print str(len(links_received)) + " " + current_follower
            current_follower = follower
            num_links_received = 0
            links_received = set()
        if followee in urls: 
            num_links_received += len(urls[followee])
            for u in urls[followee]:
                links_received.add(u)
        if followee not in num_followers:
            num_followers[followee] = 0
        num_followers[followee] += 1
# at the end
print str(len(links_received)) + " " + current_follower
